/*
 *  CECocos2dUtility.cpp
 *  LID Library
 *
 *  Created by Alexander Trutchenko on 09/11/11.
 *  Copyright 2011 League of Independent Developers. All rights reserved.
 *
 */

#include "cocos2dx_extentions/utility/CECocos2dUtility.h"

#include "cocos2dx_extentions/localization/CEDictMaker.h"

#include <math.h>

#include "cocos2d.h"


using namespace cocos2d;

namespace cocosext {

bool isNodeContainsTouch(CCNode& node, CCTouch* touch)
{	
	if (node.getIsVisible())
	{
		CCPoint touchLocation = touch->locationInView(touch->view());
		touchLocation = CCDirector::sharedDirector()->convertToGL(touchLocation);
		CCPoint local = node.convertToNodeSpace(touchLocation);
		CCRect r = CCRectMake(0, 0, node.getContentSize().width, node.getContentSize().height);
		
		if (CCRect::CCRectContainsPoint(r, local))
			return true;
	}
	
	return false;
}

CCPoint safeNormalize(const CCPoint v)
{
	float length = ccpLength(v);
	if (length > 1.f-FLT_EPSILON && length < 1.f+FLT_EPSILON) return v;
	else if (length < FLT_EPSILON) return  CCPointZero;
	else return ccpNormalize(v);
}

float clampAngleDeg(float angle)
{
	while (angle < 0) angle += 360.f;
	while (angle > 360.f) angle -= 360.f;
	
	return angle;
}

float clampAngleRad(float angle)
{
	while (angle < 0.f) angle += 2.f*(float)M_PI;
	while (angle > 2.f*(float)M_PI) angle -= 2.f*(float)M_PI;
	
	return angle;
}	

CCDictionary<std::string, CCObject*>* cocosextDictionaryWithContentsOfFile(const char *pFileName)
{
    CCDictionary<std::string, CCObject*> *ret = cocosextDictionaryWithContentsOfFileThreadSafe(pFileName);
    ret->autorelease();

    return ret;
}

CCDictionary<std::string, CCObject*>* cocosextDictionaryWithContentsOfFileThreadSafe(const char *pFileName)
{
    CEDictMaker tMaker;
    return tMaker.dictionaryWithContentsOfFile(pFileName);
}

int getHigherTouchPriority(int touchPriority)
{
	return --touchPriority;
}

void pauseNodeAndChildren(CCNode* pNode)
{
    pNode->pauseSchedulerAndActions();
    CCArray* children = pNode->getChildren();
    if (children == NULL)
        return;
    for (unsigned int i = 0; i < children->count(); ++i)
        pauseNodeAndChildren((CCNode*)pNode->getChildren()->objectAtIndex(i));
}

void resumeNodeAndChildren(CCNode* pNode)
{
    pNode->resumeSchedulerAndActions();
    CCArray* children = pNode->getChildren();
    if (children == NULL)
        return;
    for (unsigned int i = 0; i < children->count(); ++i)
        resumeNodeAndChildren((CCNode*)pNode->getChildren()->objectAtIndex(i));
}

cocos2d::CCSprite* spritePair(const char* sppParent, const char* sppChild)
{
	CCSprite* parent = CCSprite::spriteWithSpriteFrameName(sppParent);
	CCSprite* child = CCSprite::spriteWithSpriteFrameName(sppChild);
	child->setPosition(ccpMid(parent->getContentSize()));
	parent->addChild(child);
	return parent;
}

cocos2d::CCNode* spritePair(cocos2d::CCNode* sprParent, cocos2d::CCNode* sprChild)
{
	sprChild->setPosition(ccpMid(sprParent->getContentSize()));
	sprParent->addChild(sprChild);
	return sprParent;
}

	/** Open site with url */
void openSiteWithURL(const char* url)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_BADA)
    
    char* fullurl = (char*)malloc((strlen(url) + 5) * sizeof(char));
    memset(fullurl, 0x00, (strlen(url) + 5));
    sprintf(fullurl, "url:%s", url);
    Osp::Base::Collection::ArrayList* pDataList = null;
    pDataList = new Osp::Base::Collection::ArrayList();
    pDataList->Construct();
    Osp::Base::String str(fullurl);
    pDataList->Add(str);
    free(fullurl);
    
    Osp::App::AppControl* pAc =  Osp::App::AppManager::FindAppControlN(Osp::App::APPCONTROL_BROWSER, Osp::Base::String());
    if(pAc)
    {
        pAc->Start(pDataList, null);
        
    }
    
    //TODO: Alex T.: Not correct! Create control wrapper
    
    //	pDataList->RemoveAll(true);
    //	delete pDataList;
    //	delete pAc;
    
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
    
    char* fullurl = (char*)malloc((strlen(url) + 5) * sizeof(char));
    memset(fullurl, 0x00, (strlen(url) + 5));
    sprintf(fullurl, "url:%s", url);
    free(fullurl);
    ShellExecuteA(0,0,fullurl,0,0,SW_SHOWNORMAL);
    
#elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    
//    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithUTF8String:url]]];
    
#endif // CC_PLATFORM_WIN32
}


}; // end namespace cocosext

